﻿using System;
using Gumini.JulaDB.Planner;

namespace Gumini.JulaDB.Expressions
{
    internal class ColumnNameGenerator
    {
        int _columnNumber = 0;

        public string GetColumnName(ColumnExpressionItem item)
        {
            string columnName;

            // if the expression is a simple reference to a table column
            // the resulting column will have the same name as the source column
            var variable = item.Expression as Variable;
            if (variable != null)
                columnName = item.Alias ?? variable.Identifier;
            else
                columnName = item.Alias;

            // otherwise we generate a name for this column
            columnName = columnName ?? String.Format("column_{0}", _columnNumber++);

            return columnName;
        }
    }
}
